﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceManager.DataAccessLayer
{
    public class GroupDiveceDbOperation:IDatabaseOperation<NHOMTHIETBI>
    {
        QLThietBiEntities devicemanager;
        #region IDatabaseOperation<T> Members

        public int SaveRecord(NHOMTHIETBI record)
        {
            try
            {
                using (devicemanager = new QLThietBiEntities())
                {
                    devicemanager.NHOMTHIETBIs.Add(record);
                    devicemanager.SaveChanges();
                }
                return record.NhomTBID;
            }
            catch
            {
                return -1;
            }
        }

        public bool UpdateRecord(NHOMTHIETBI record)
        {
            using (devicemanager = new QLThietBiEntities())
            {
                devicemanager.NHOMTHIETBIs.Find(record.NhomTBID);
                devicemanager.SaveChanges();
                return true;
            }
        }

        public List<NHOMTHIETBI> GetRecord(NHOMTHIETBI record)
        {
            devicemanager = new QLThietBiEntities();
            IQueryable<NHOMTHIETBI> custQuery = devicemanager.NHOMTHIETBIs.AsQueryable<NHOMTHIETBI>();
            if (!string.IsNullOrEmpty(record.TenNhomTB))
                custQuery = custQuery.Where(c => c.TenNhomTB.Contains(record.TenNhomTB));
            return custQuery.ToList();
        }

        public bool DeleteRecord(NHOMTHIETBI record)
        {

            using (devicemanager = new QLThietBiEntities())
            {
                var cust = devicemanager.NHOMTHIETBIs.FirstOrDefault(c => c.NhomTBID == record.NhomTBID);
                devicemanager.NHOMTHIETBIs.Remove(cust);
                devicemanager.SaveChanges();
                return true;
            }
        }

        #endregion
    }
}
